package testcase.app;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import api.app.AddFeedback;
import api.app.ApiUploadPicforFeedback;
import common.Host;
import common.ImageOperation;
import common.TestData;

/**
 * 帮忙医APP提交意见反馈AddFeedback接口测试用例
 * 
 * @author lipeng
 *
 */
public class TestAddFeedback
{
	/**
	 * 帮忙医APP提交意见反馈AddFeedback接口测试环境数据驱动
	 * 
	 * @return --测试数据
	 * @throws Exception
	 */
	@DataProvider(name = "Test")
	public static String[][] testAddFeedbackDataProvider() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestAddFeedback.xml");
		return td.getParam();
	}

	/**
	 * 帮忙医APP提交意见反馈AddFeedback接口
	 * 
	 * @param casename  --用例名称
	 * @param token     --token
	 * @param AttachPics --上传图片
	 * @param FeedbackBody--上传意见
	 * @throws Exception
	 */
	@Test(dataProvider = "Test", groups = { "Test" })
	public void testAddFeedbackTestCase(String casename, String token, String AttachPics, String FeedbackBody)
			throws Exception
	{
		String[] pictureArray = AttachPics.split(",");

		if (!(pictureArray.length == 1 && pictureArray[0].isEmpty()))
		{
			for (int i = 0; i < pictureArray.length; i++)
			{
				String base64img = ImageOperation.base64Encode("resources/image/" + pictureArray[i]);
				String apiRet = ApiUploadPicforFeedback.post(base64img);
				JSONObject jsonObject = new JSONObject(apiRet);
				pictureArray[i] = jsonObject.getJSONArray("path").getString(0);
			}
		}
		else
		{
			pictureArray = null;
		}

		// 调用接口并将接口返回的数据转换成json格式
		String result = AddFeedback.post(token, pictureArray, FeedbackBody);
		JSONObject jsonResult = new JSONObject();
		if (result == null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}

		// 设置检查点
		switch (casename)
		{
			case "Success":
				Assert.assertEquals(jsonResult.getString("Data"), "true");
				break;
			case "Success1":
				Assert.assertEquals(jsonResult.getString("Data"), "true");
				break;
			case "NoPic":
				Assert.assertEquals(jsonResult.getString("Data"), "true");
				break;
			case "NoBody":
				Assert.assertEquals(jsonResult.getString("Data"), "true");
				break;
			default:
				break;
		}

	}
}
